package bb.lanxing.util;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import bb.lanxing.R;
import bb.lanxing.activity.mine.MedalDetailActivity;
import bb.lanxing.model.database.Medal;
import bb.lanxing.model.json.UserAvatarMedal;
import bb.lanxing.network.MyHttpClient;
import bb.lanxing.network.NetSubscribe;
import bb.lanxing.util.image.GlideUtil;
import bb.lanxing.util.image.MedalImageLoadUtil;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class MedalUtil {
    final private static String TAG = "MedalUtil";

    public static int getLevel(int hotValue) {
        if (hotValue < 100) {
            return 0;
        }
        if (hotValue >= 1000){
            return 3;
        }

        if (hotValue >= 300) {

            return 2;
        }

        return 1;
    }

    public static int getTargetLevelValue(int hotValue) {
        if (hotValue < 100) {
            return 100;
        }
        if (hotValue >= 300){
            return 1000;
        }
        else{
            return 300;
        }
    }

    public static List<String> getLevelImageList(Medal medal) {
        String detail = medal.getDetail();
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add(medal.getExpiredPic());
        try {
            JSONArray jSONArray = new JSONArray(detail);
            for (int i = 0; i < jSONArray.length(); i++) {
                arrayList.add(JsonUtil.getStringValue("url", jSONArray.getJSONObject(i)));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return arrayList;
    }

    public static String getTargetLevelImage(int hotValue, Medal medal) {
        List<String> levelImageList = getLevelImageList(medal);
        if (levelImageList.size() < 3) {
            return null;
        }
        int level = getLevel(hotValue);
        if (level != 0) {
            if (level != 2) {
                if (level != 3) {
                    return levelImageList.get(3);
                }
                return levelImageList.get(1);
            }
            return levelImageList.get(2);
        }
        return levelImageList.get(0);
    }

    public static void showAvatarMedal(String medalUrls, String plateNum, TextView tvPlateNum, ImageView... views) {
        if (views == null || views.length == 0) {
            return;
        }
        String[] split = TextUtils.isEmpty(medalUrls) ? null : medalUrls.split(";");
        for (int i = 0; i < views.length; i++) {
            if (split != null && i < split.length) {
                views[i].setVisibility(View.VISIBLE);
                Log.d(TAG, split[i] + "!medalSmall");
                MedalImageLoadUtil.showMedal((Context) null, split[i], views[i], 2);
            } else {
                views[i].setVisibility(View.GONE);
            }
        }
        if (TextUtils.isEmpty(plateNum)) {
            tvPlateNum.setVisibility(View.GONE);
            return;
        }
        tvPlateNum.setVisibility(View.VISIBLE);
        tvPlateNum.setText(plateNum);
        tvPlateNum.setTextSize(1, plateNum.length() < 5 ? 10.0f : 8.0f);
    }

    public static void showAvatarMedal(String medalUrls, ViewGroup medalContainer, String plateNum,
                                       boolean isPro, final Context context, final List<UserAvatarMedal> userAvatarMedals,
                                       String licenseNumberskinUrl, String licenseNumberTextColor) {
        ArrayList<ImageView> arrayList = new ArrayList<>();
        ImageView addMedal1 = medalContainer.findViewById(R.id.addMedalShowOne);
        if (addMedal1 != null) {
            arrayList.add(addMedal1);
        }
        ImageView addMedal2 = medalContainer.findViewById(R.id.addMedalShowTwo);
        if (addMedal2 != null) {
            arrayList.add(addMedal2);
        }
        ImageView addMedal3 = medalContainer.findViewById(R.id.addMedalShowThree);
        if (addMedal3 != null) {
            arrayList.add(addMedal3);
        }
        if (!arrayList.isEmpty()) {
            String[] split = TextUtils.isEmpty(medalUrls) ? null : medalUrls.split(";");
            for (int i = 0; i < arrayList.size(); i++) {
                if (split != null && i < split.length) {
                    arrayList.get(i).setVisibility(View.VISIBLE);
                    MedalImageLoadUtil.showMedal(null, split[i], arrayList.get(i), 2);
                    if (userAvatarMedals != null) {
                        final UserAvatarMedal userAvatarMedal = userAvatarMedals.get(i);
                        arrayList.get(i).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                MedalUtil.showAvatorMedalDetail(context, userAvatarMedal.getMedalId(), userAvatarMedal.getMedalLevel());
                            }
                        });
                    }
                } else {
                    arrayList.get(i).setVisibility(View.GONE);
                }
            }
        }

        TextView userPlate = medalContainer.findViewById(R.id.user_plate_number);
        if (userPlate != null) {
            if (TextUtils.isEmpty(plateNum)) {
                userPlate.setVisibility(View.GONE);
            } else {
                userPlate.setTextSize(1, plateNum.length() < 5 ? 10.0f : 8.0f);
                userPlate.setTextColor(context.getResources().getColor(R.color.global_blue_color));
                userPlate.setText(plateNum);
                userPlate.setBackgroundResource(R.drawable.ic_default);
                if (licenseNumberskinUrl != null && !licenseNumberskinUrl.isEmpty()
                        && licenseNumberTextColor != null && !licenseNumberTextColor.isEmpty()) {
                    GlideUtil.getInstance().showImage(licenseNumberskinUrl, userPlate, R.drawable.ic_default);
                    if (licenseNumberTextColor != null
                            && !licenseNumberTextColor.isEmpty()
                            && !licenseNumberTextColor.equals("null")) {
                        userPlate.setTextColor(Color.parseColor(licenseNumberTextColor));
                    }
                }
                userPlate.setVisibility(View.VISIBLE);
            }
        }
    }

    public static void showAvatorMedalDetail(final Context context, long medalId, int medalLevel) {
        Medal byId = Medal.getById(medalId);
        if (byId == null) {
            Observable.create(new NetSubscribe(MyHttpClient
                    .requestMedalDetail(medalId, medalLevel)))
                    .subscribeOn(Schedulers.io()).flatMap(new Func1<String, Observable<Medal>>() {
                        @Override
                        public Observable<Medal> call(String s) {
                            Medal medal = null;
                            try {
                                JSONObject objectValue = JsonUtil.getObjectValue("data", new JSONObject(s));
                                if (objectValue != null) {
                                    Medal medal2 = new Medal(objectValue);
                                    medal2.setIsmm(1);
                                    medal2.save();
                                    medal = medal2;
                                }
                            } catch (JSONException e2) {
                                e2.printStackTrace();
                            }
                            return Observable.just(medal);
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<>() {
                        @Override
                        public void onCompleted() {
                        }

                        @Override
                        public void onError(Throwable e) {
                        }

                        @Override
                        public void onNext(Medal medal) {
                            Intent intent = new Intent(context, MedalDetailActivity.class);
                            intent.putExtra("medal", medal);
                            context.startActivity(intent);
                        }
                    });
            return;
        }
        byId.setIsmm(1);
        Intent intent = new Intent(context, MedalDetailActivity.class);
        intent.putExtra("medal", byId);
        context.startActivity(intent);
    }

    public static List<UserAvatarMedal> getUserAvatorMedalInfo(JSONArray jsonArray) {
        ArrayList<UserAvatarMedal> arrayList = new ArrayList<>();
        if (jsonArray.length() > 0) {
            for (int i = 0; i < jsonArray.length(); i++) {
                try {
                    arrayList.add(new UserAvatarMedal(jsonArray.getJSONObject(i)));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        if (arrayList.size() > 0) {
            return arrayList;
        }
        return null;
    }
}